package liuyuyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import liuyuyang.domain.User;
import liuyuyang.domain.UserRole;
import liuyuyang.mapper.RoleMapper;
import liuyuyang.mapper.UserMapper;
import liuyuyang.mapper.UserRoleMapper;
import liuyuyang.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public User login(String username, String password) throws Exception {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        // 条件查询的属性名必须是数据库中的属性名，而不是实体类中的属性名
        queryWrapper.eq("user_name", username);
        User u = userMapper.selectOne(queryWrapper);

        if (u != null) {
            // queryWrapper.eq("user_name", username);
            queryWrapper.eq("password", password);

            // 只能查出一个用户，因为用户名是唯一的
            User user = userMapper.selectOne(queryWrapper);

            if (user != null) return user;

            throw new Exception("密码不正确");
        } else {
            throw new Exception("用户名不存在");
        }
    }

    @Override
    public void reset(Integer uid) {
        User user = userMapper.selectById(uid);

        // 重置密码为123456
        user.setPassword("123456");
        userMapper.updateById(user);
    }

    @Override
    public void edit(User user) {
        Integer n = userMapper.updateById(user);
        System.out.println(n);
    }

    @Override
    public void editPass(User user) throws Exception {
        User data = userMapper.selectById(user.getUid());

        String pass = data.getPassword();
        String oldPass = user.getOldPassword();
        String newPass = user.getNewPassword();

        // 修改密码之前先判断旧密码是否正确
        if (Objects.equals(pass, oldPass)) {
            data.setPassword(newPass);

            userMapper.updateById(data);
        } else {
            throw new Exception("修改失败：旧密码不正确");
        }
    }

    @Override
    public User get(Integer uid) {
        return userMapper.selectById(uid);
    }

    @Override
    public List<User> list() {
        // 查询所有用户
        List<User> list = userMapper.selectList(null);

        for (User user : list) {
            // 根据ID找到每个用户所对应的权限
            QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("user_id", user.getUid());
            List<UserRole> userRoleList = userRoleMapper.selectList(queryWrapper);

            // 关联权限
            for (UserRole userRole : userRoleList) {
                user.getRole().add(roleMapper.selectById(userRole.getRoleId()));
            }
        }

        return list;
    }
}
